package com.ruoyi.erp.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.erp.domain.dto.ErpWeeklyDTO;
import com.taobao.api.ApiException;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.erp.domain.ErpWeekly;
import com.ruoyi.erp.service.IErpWeeklyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 周报数据Controller
 *
 * @author yanwenliang
 * @date 2024-10-14
 */
@RestController
@RequestMapping("/erp/weekly")
public class ErpWeeklyController extends BaseController
{
    @Autowired
    private IErpWeeklyService erpWeeklyService;

    /**
     * 查询周报数据列表
     */
    @PreAuthorize("@ss.hasPermi('erp:weekly:list')")
    @GetMapping("/list")
    public TableDataInfo list(ErpWeeklyDTO erpWeeklyDTO) {
        startPage();
        List<ErpWeeklyDTO> list = erpWeeklyService.selectErpWeeklyList(erpWeeklyDTO);
        return getDataTable(list);
    }

    /**
     * 导出周报数据列表
     */
    @PreAuthorize("@ss.hasPermi('erp:weekly:export')")
    @Log(title = "周报数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ErpWeeklyDTO erpWeeklyDTO)
    {
        List<ErpWeeklyDTO> list = erpWeeklyService.selectErpWeeklyList(erpWeeklyDTO);
        List<ErpWeekly> erpWeeklyList = BeanUtils.convertList(list, ErpWeekly.class);
        ExcelUtil<ErpWeekly> util = new ExcelUtil<ErpWeekly>(ErpWeekly.class);
        util.exportExcel(response, erpWeeklyList, "周报数据数据");
    }

    /**
     * 获取周报数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('erp:weekly:query')")
    @GetMapping(value = "/{weeklyId}")
    public AjaxResult getInfo(@PathVariable("weeklyId") String weeklyId)
    {
        return success(erpWeeklyService.selectErpWeeklyByWeeklyId(weeklyId));
    }

    /**
     * 新增周报数据
     */
    @PreAuthorize("@ss.hasPermi('erp:weekly:add')")
    @Log(title = "周报数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ErpWeekly erpWeekly) throws ApiException {
        return toAjax(erpWeeklyService.insertErpWeekly(erpWeekly));
    }

    /**
     * 修改周报数据
     */
    @PreAuthorize("@ss.hasPermi('erp:weekly:edit')")
    @Log(title = "周报数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ErpWeekly erpWeekly)
    {
        return toAjax(erpWeeklyService.updateErpWeekly(erpWeekly));
    }

    /**
     * 删除周报数据
     */
    @PreAuthorize("@ss.hasPermi('erp:weekly:remove')")
    @Log(title = "周报数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{weeklyIds}")
    public AjaxResult remove(@PathVariable String[] weeklyIds)
    {
        return toAjax(erpWeeklyService.deleteErpWeeklyByWeeklyIds(weeklyIds));
    }

    /**
     * 获取周报数据
     * @return
     */
    @PostMapping("getLatestWeekly")
    public AjaxResult getLatestWeekly(@RequestBody ErpWeekly erpWeekly){
        return AjaxResult.success(erpWeeklyService.getLatestWeekly(erpWeekly));
    }

    /**
     * 周报统计
     * @return
     */
    @PostMapping("weeklyStatistics")
    public AjaxResult weeklyStatistics(){
        return AjaxResult.success(erpWeeklyService.weeklyStatistics());
    }

}
